﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/3/9 11:30:50
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageK.Plugin
{
    /// <summary>
    /// This plugin implements most of the Edit/Options/Colors command.
    /// </summary>
    public class Colors
    {
        public static string[] colors = {"red","green","blue","magenta","cyan","yellow","orange","black","white","gray","lightgray","darkgray","pink"};
        private static string[] colors2 = {"Red","Green","Blue","Magenta","Cyan","Yellow","Orange","Black","White","Gray","lightGray","darkGray","Pink"};

        /** For named colors, returns the name, or 'defaultName' if not a named color.
        *  'color' must not be null. */
        private static string getColorName(Color c, string defaultName, bool useCapitalizedName)
        {
            string[] colorNames = useCapitalizedName ? colors2 : colors;
            if (c.Equals(Color.Red)) return colorNames[0];
            else if (c.Equals(Color.Green)) return colorNames[1];
            else if (c.Equals(Color.Blue)) return colorNames[2];
            else if (c.Equals(Color.Magenta)) return colorNames[3];
            else if (c.Equals(Color.Cyan)) return colorNames[4];
            else if (c.Equals(Color.Yellow)) return colorNames[5];
            else if (c.Equals(Color.Orange)) return colorNames[6];
            else if (c.Equals(Color.Black)) return colorNames[7];
            else if (c.Equals(Color.White)) return colorNames[8];
            else if (c.Equals(Color.Gray)) return colorNames[9];
            else if (c.Equals(Color.LightGray)) return colorNames[10];
            else if (c.Equals(Color.DarkGray)) return colorNames[11];
            else if (c.Equals(Color.Pink)) return colorNames[12];
            return defaultName;
        }

        /** For named colors, converts the name String to the corresponding color.
         *  Returns 'defaultColor' if the color has no name.
         *  Use 'decode' to also decode hex color names like "#ffff00" */
        public static Color getColor(string name, Color defaultColor)
        {
            if (name == null || name.Length < 2)
                return defaultColor;
            // name = name.ToLower(Locale.US);
            name = name.ToLower();
            Color c = defaultColor;
            if (name.Contains(colors[7])) c = Color.Black;
            else if (name.Contains(colors[8])) c = Color.White;
            else if (name.Contains(colors[0])) c = Color.Red;
            else if (name.Contains(colors[2])) c = Color.Blue;
            else if (name.Contains(colors[5])) c = Color.Yellow;
            else if (name.Contains(colors[1])) c = Color.Green;
            else if (name.Contains(colors[3])) c = Color.Magenta;
            else if (name.Contains(colors[4])) c = Color.Cyan;
            else if (name.Contains(colors[6])) c = Color.Orange;
            else if (name.Contains(colors[12])) c = Color.Pink;
            else if (name.Contains(colors[9]) || name.Contains("grey"))
            { //gray or grey
                if (name.Contains("light")) c = Color.LightGray;
                else if (name.Contains("dark")) c = Color.DarkGray;
                else c = Color.Gray;
            }
            return c;
        }

        /** Converts a Color into a lowercase string ("red", "green", "#aa55ff", etc.).
        *  If <code>color</code> is <code>null</code>, returns the String "none". */
        public static String colorToString(Color color)
        {
            if (color == null) return "none";
            String str = getColorName(color, null, false);
            if (str == null)
                str = "#" + getHexString(color);
            return str;
        }

        /** Returns the 6-digit hex string such as "aa55ff" for opaque colors or
         *  or 8-digit like "80aa55ff" for other colors (the first two hex digits are alpha).
         *  'color' must not be null. */
        private static String getHexString(Color color)
        {
            int rgb = color.ToArgb();
            bool isOpaque = (rgb & 0xff000000) == 0xff000000;
            if (isOpaque)
                rgb &= 0x00ffffff;  //don't show alpha for opaque colors
            string format = isOpaque ? "%06x" : "%08x";
            return string.Format(format, rgb);
        }
    }
}
